package com.blog.service.Impl;
import com.blog.Utils.Result;
import com.blog.base.ThreadLocalUserId;
import com.blog.bean.domain.Note;
import com.blog.bean.dto.NoteDTO;
import com.blog.bean.vo.NoteVO;
import com.blog.exception.ServiceException;
import com.blog.mapper.NoteMapper;
import com.blog.service.NoteService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 任浩
 * @version 1.0
 */

@Slf4j
@Service
public class NoteServiceImpl implements NoteService {

    @Autowired
    private NoteMapper noteMapper;

    @Override
    public Map<String, Object> getNote(String startTime, String endTime, String type, String title,Integer size,Integer page) {
        if(size == null || page == null){
            log.error("页数和页数大小不能为空");
            throw new ServiceException(400,"页数和页数大小不能为空");
        }
        PageHelper.startPage(page,size);
        Page<NoteVO> pageData = noteMapper.getNoteList(startTime,endTime,type,title);
        //转换返回给前端显示的数据
        List<NoteVO> noteVOList = new ArrayList<>();
        pageData.getResult().forEach(note -> {
            String description = note.getContent().length()> 50 ? note.getContent().substring(0,50) : note.getContent();
            NoteVO build = new NoteVO.NoteVOBuilder().id(note.getId())
                    .titleUrl(note.getTitleUrl())
                    .type(note.getType())
                    .content(note.getContent())
                    .title(note.getTitle())
                    .updateTime(note.getUpdateTime())
                    .userNmae(note.getUserName())
                    .description(description).build();
            noteVOList.add(build);
        });

        Map<String,Object> data = new HashMap<>();
        data.put("data",noteVOList);
        data.put("total",pageData.getTotal());
        return data;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result editNote(NoteDTO noteDTO) {
        Note note = new Note();
        log.info("拷贝前的note为:{}",noteDTO);
        BeanUtils.copyProperties(noteDTO,note);
        log.info("拷贝过后的note:{}",note);

        if(note.getId() == null){  //这是新增的操作
            log.info("这是新增操作");
            Integer userId = Integer.valueOf(ThreadLocalUserId.get());
            note.setUserId(userId);
            note.setUpdateTime(LocalDateTime.now());
            int insert = noteMapper.insert(note);
            if(insert != 1){
                log.error("插入数据失败");
                return Result.error("笔记插入数据失败");
            }
        }
        else{  //这是更新的操作
            log.info("这是更新操作");
            if(note.getUpdateTime() == null){
                note.setUpdateTime(LocalDateTime.now());
            }
            int update = noteMapper.updateById(note);
            if(update != 1){
                log.error("笔记更新数据失败");
                return Result.error("笔记更新数据失败");
            }
        }
        return Result.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteNote(List<Integer> ids) {
        try {
            noteMapper.deleteBatchIds(ids);
        }
        catch (Exception e){
            e.printStackTrace();
            return Result.error("笔记删除失败");
        }
        return null;
    }
}
